package com.xteam.ecsystem.dao.Impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.xteam.ecsystem.dao.MessageDao;
import com.xteam.ecsystem.entities.BaseReplyInfo;
import com.xteam.ecsystem.utils.PageCenterStation;

@Repository
@SuppressWarnings("unchecked")
public class MessageDaoImpl extends DDLPrefix implements MessageDao {
	
	@Autowired
	private BaseDaoImpl baseDao;

	public String getMessageList(int pageIndex, int pageSize, String bumName,
			String bumType, String bumTitle, String bumState, String isreply) {
		JSONObject jo = new JSONObject();
		try {
			List list = new ArrayList();
			String hql = SQL_FROM + "BaseUserMessage um" + SQL_WHERE + " 1=1 ";
			String countHQL = SQL_SELECT + " count(*) " + SQL_FROM
					+ "BaseUserMessage" + SQL_WHERE + " 1=1 ";
			if (isreply != null && isreply.length() > 0
					&& !isreply.equals("undefined") && !isreply.equals("null")) {
				if (Integer.parseInt(isreply) == 1) {
					hql = SQL_FROM
							+ "BaseUserMessage um "
							+ SQL_WHERE
							+ " 1=1 "
							+ SQL_AND
							+ " um.bumId in (select ri.briTarget from BaseReplyInfo ri where ri.briType=1)";
					countHQL = SQL_SELECT
							+ " count(*) "
							+ SQL_FROM
							+ "BaseUserMessage um "
							+ SQL_WHERE
							+ " 1=1 "
							+ SQL_AND
							+ " um.bumId in (select ri.briTarget from BaseReplyInfo ri where ri.briType=1)";
				} else {
					hql = SQL_FROM
							+ "BaseUserMessage um"
							+ SQL_WHERE
							+ " 1=1 "
							+ SQL_AND
							+ " um.bumId not in (select ri.briTarget from BaseReplyInfo ri where ri.briType=1)";
					countHQL = SQL_SELECT
							+ " count(*) "
							+ SQL_FROM
							+ "BaseUserMessage um"
							+ SQL_WHERE
							+ " 1=1 "
							+ SQL_AND
							+ " um.bumId not in (select ri.briTarget from BaseReplyInfo ri where ri.briType=1)";
				}
			}
			if (bumName != null && bumName.length() > 0
					&& !bumName.equals("undefined") && !bumName.equals("null")) {
				hql += SQL_AND + " um.bumName " + SQL_LIKE + " ? ";
				countHQL += SQL_AND + " um.bumName " + SQL_LIKE + " ? ";
				list.add("%" + bumName + "%");
			}

			if (bumTitle != null && bumTitle.length() > 0
					&& !bumTitle.equals("undefined")
					&& !bumTitle.equals("null")) {
				hql += SQL_AND + " um.bumTitle " + SQL_LIKE + " ? ";
				countHQL += SQL_AND + " um.bumTitle " + SQL_LIKE + " ? ";
				list.add("%" + bumTitle + "%");
			}
			if (bumState != null && bumState.length() > 0
					&& !bumState.equals("undefined")
					&& !bumState.equals("null") && !bumState.equals("-1")) {
				hql += SQL_AND + " um.bumState =? ";
				countHQL += SQL_AND + " um.bumState =? ";
				list.add(Integer.parseInt(bumState));
			}

			PageCenterStation<?> p = (PageCenterStation<?>) this.baseDao
					.queryList(countHQL, hql, list.toArray(), pageIndex * pageSize, pageSize);
			jo = JSONObject.fromObject(p);
			/*
			 * JSONArray array = jo.getJSONArray("data"); JSONArray datas = new
			 * JSONArray(); for (int i = 0; i < array.size(); i++) { JSONObject
			 * c = (JSONObject) array.get(i); Object obj =
			 * c.getJSONArray("reply"); if (obj instanceof JSONArray) { if
			 * (((JSONArray) obj).size() > 0) { c.remove("reply");
			 * c.element("isreply", "1"); } else { c.remove("reply");
			 * c.element("isreply", "0"); } } datas.add(c); } jo.remove("data");
			 * jo.element("data", datas);
			 */

		} catch (Exception e) {
			e.printStackTrace();
		}
		return jo.toString();
	}

	public void savereply(String name, String email, String bumId,
			String reply, String ipadd) {
		try {
			BaseReplyInfo ReplyInfo = new BaseReplyInfo();
			ReplyInfo.setBriContent(reply);
			ReplyInfo.setBriEmail(email);
			ReplyInfo.setBriIp(ipadd);
			ReplyInfo.setBriName(name);
			ReplyInfo.setBriTarget(bumId);
			ReplyInfo.setBriType(1);
			ReplyInfo.setBriTime(new Date());
			baseDao.save(ReplyInfo);
		} catch (Exception e) {

		}

	}

	public void saveApprove(String bumId,String state) {
		String sql = "update base_user_message set bum_state='"+state+"' where bum_id='"
			+ bumId + "'";
		try {
			baseDao.executeUpdateWithSQL(sql, (Object[]) null);
		} catch (Exception E) {
		}
	}

	public void delMessage(String bumId) {
		String oId[] = bumId.split(",");
		for (String id : oId) {
			try {
				String sql = "delete from base_user_message where bum_id in ("
						+ id + ")";
				baseDao.executeUpdateWithSQL(sql, (Object[]) null);

			} catch (Exception E) {
			}
		}
	}

	public String findMessageReply(String bumId) {
		JSONArray array = new JSONArray();
		try {
			String sql = "from BaseReplyInfo where briTarget='" + bumId + "'";
			List list = baseDao.find(sql);
			array = JSONArray.fromObject(list);
		} catch (Exception E) {
		}
		return array.toString();
	}

	public void deleteReply(String briId) {
		try {
			String sql = "delete from base_reply_info where bri_id='" + briId
					+ "'";
			baseDao.executeUpdateWithSQL(sql, (Object[]) null);

		} catch (Exception E) {
		}
	}

	public void updateReply(String briId, String briContent) {
		try {
			String sql = "update base_reply_info set bri_time =CURRENT_TIMESTAMP, bri_content='"
					+ briContent + "' where bri_id='" + briId + "'";
			baseDao.executeUpdateWithSQL(sql, (Object[]) null);

		} catch (Exception E) {
		}
	}
}
